﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using GBFinance.Entity;
using HiLand.Framework.FoundationLayer;
using HiLand.Utility.DataBase;

namespace GBFinance.DAL
{
    public class LiabilityDAL : BaseSqlDAL<LiabilityEntity>
    {
        #region 基本信息
        protected override string TableName
        {
            get { return "LoanLiability"; }
        }

        protected override string[] KeyNames
        {
            get { return new string[] { "LiabilityGuid" }; }
        }

        /// <summary>
        /// Guid主键名称
        /// </summary>
        protected override string GuidKeyName
        {
            get { return "LiabilityGuid" ; }
        }

        protected override string PagingSPName
        {
            get { return "usp_Loan_Liability_SelectPaging"; }
        }
        #endregion

        public override bool Create(LiabilityEntity entity)
        {
            string commandText = @"Insert Into [LoanLiability] (
			        [LiabilityGuid],
			        [ItemKind],
			        [ItemType],
			        [LiabilityLenderInfo],
			        [LiabilityAmountOwing],
			        [LiabilityPaymentMonthly],
			        [UserGuid],
			        [PropertyNames],
			        [PropertyValues]
                ) 
                Values (
			        @LiabilityGuid,
			        @ItemKind,
			        @ItemType,
			        @LiabilityLenderInfo,
			        @LiabilityAmountOwing,
			        @LiabilityPaymentMonthly,
			        @UserGuid,
			        @PropertyNames,
			        @PropertyValues
                )";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.Instance.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        public override bool Update(LiabilityEntity entity)
        {
            string commandText = @"Update [LoanLiability] Set   
					[LiabilityGuid] = @LiabilityGuid,
					[ItemKind] = @ItemKind,
					[ItemType] = @ItemType,
					[LiabilityLenderInfo] = @LiabilityLenderInfo,
					[LiabilityAmountOwing] = @LiabilityAmountOwing,
					[LiabilityPaymentMonthly] = @LiabilityPaymentMonthly,
					[UserGuid] = @UserGuid,
					[PropertyNames] = @PropertyNames,
					[PropertyValues] = @PropertyValues
             Where [LiabilityGuid] = @LiabilityGuid";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.Instance.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }
        #region 辅助方法
        protected override void InnerPrepareParasAll(LiabilityEntity entity, ref List<SqlParameter> paraList)
        {
            List<SqlParameter> list = new List<SqlParameter>() 
            {
			    new SqlParameter("@LiabilityGuid",entity.LiabilityGuid== Guid.Empty?Guid.NewGuid():entity.LiabilityGuid),
			    new SqlParameter("@ItemKind",entity.ItemKind),
			    new SqlParameter("@ItemType",entity.ItemType),
			    new SqlParameter("@LiabilityLenderInfo",entity.LiabilityLenderInfo??string.Empty),
			    new SqlParameter("@LiabilityAmountOwing",entity.LiabilityAmountOwing),
			    new SqlParameter("@LiabilityPaymentMonthly",entity.LiabilityPaymentMonthly),
			    new SqlParameter("@UserGuid",entity.UserGuid)
            };

            paraList.AddRange(list);
        }

        protected override LiabilityEntity Load(IDataReader reader)
        {
            LiabilityEntity entity = new LiabilityEntity();
            if (reader != null && reader.IsClosed == false)
            {
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "LiabilityID"))
                {
                    entity.LiabilityID = reader.GetInt32(reader.GetOrdinal("LiabilityID"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "LiabilityGuid"))
                {
                    entity.LiabilityGuid = reader.GetGuid(reader.GetOrdinal("LiabilityGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemKind"))
                {
                    entity.ItemKind = reader.GetInt32(reader.GetOrdinal("ItemKind"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemType"))
                {
                    entity.ItemType = reader.GetInt32(reader.GetOrdinal("ItemType"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "LiabilityLenderInfo"))
                {
                    entity.LiabilityLenderInfo = reader.GetString(reader.GetOrdinal("LiabilityLenderInfo"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "LiabilityAmountOwing"))
                {
                    entity.LiabilityAmountOwing = reader.GetDecimal(reader.GetOrdinal("LiabilityAmountOwing"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "LiabilityPaymentMonthly"))
                {
                    entity.LiabilityPaymentMonthly = reader.GetDecimal(reader.GetOrdinal("LiabilityPaymentMonthly"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "UserGuid"))
                {
                    entity.UserGuid = reader.GetGuid(reader.GetOrdinal("UserGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyNames"))
                {
                    entity.PropertyNames = reader.GetString(reader.GetOrdinal("PropertyNames"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyValues"))
                {
                    entity.PropertyValues = reader.GetString(reader.GetOrdinal("PropertyValues"));
                }
            }
            return entity;
        }
        #endregion
    }
}
